import { Component } from '@angular/core';
import { NavParams, ViewController, ModalController } from 'ionic-angular';
import { TaskDetailService } from './detail.service';
import { Config } from '../../../providers/config';
import { ZcmxPage } from './zcmx.component';
import _ from 'lodash';


@Component({
  selector: 'page-task-zcmxList',
  templateUrl: './zcmxList.html',
  providers: [TaskDetailService]
})
export class ZcmxListPage {
	moa_page_name: any = "page-task-zcmxList";
  moa_page_explain: any = "流程 - 资产明细信息";

  _ = _;
  // 流程实例ID
  zcbosid;
  zcmsList = [];

  constructor(
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public detailService: TaskDetailService,
    public modalCtrl: ModalController,
  ) {
    this.zcbosid = this.navParams.get('zcbosid');
    //console.log(this.zcbosid);
    this.detailService.getZcmxList(this.zcbosid).subscribe(data => {
      console.log(data);
      if(data){
        this.zcmsList = data;
      }
    })

  }

  complete() {
    this.dismiss();
  }

  dismiss(data?: any) {
    // using the injected ViewController this page
    // can "dismiss" itself and pass back data
    this.viewCtrl.dismiss(data);
  }

  checkIsSelected(item) {
    item.isShow = !item.isShow;
  }

  // "资产分类":"",
  //       "资产名称":"",
  //       "资产单价":"",
  //       "使用部门":"",
  //       "使用人":"",
  //       "存放地点":""
  dataFomate(dataList) {
    var that = this;

    _.forEach(dataList, function(data) {
      let entryList = data.fieldValueMap.entry;
      let tempList = [];

      let temp = {};
      temp["fname"] = "资产分类";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "TREENO";
      })["value"]["$value"];
      tempList.push(temp);

      temp = {};
      temp["fname"] = "资产名称";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "ZCNAME";
      })["value"]["$value"];
      tempList.push(temp);

      temp = {};
      temp["fname"] = "资产单价";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "NETVALUE";
      })["value"]["$value"];
      tempList.push(temp);

      temp = {};
      temp["fname"] = "使用部门";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "USERPARTID";
      })["value"]["$value"];
      tempList.push(temp);

      temp = {};
      temp["fname"] = "使用人";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "USERID";
      })["value"]["$value"];
      tempList.push(temp);

      temp = {};
      temp["fname"] = "存放地点";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "OSALER";
      })["value"]["$value"];
      tempList.push(temp);

      temp = {};
      temp["fname"] = "详情ID";
      temp["fvalue"] = _.find(entryList, function(entry) {
        return entry["key"]["$value"] == "GLBOSID";
      })["value"]["$value"];
      tempList.push(temp);

      that.zcmsList.push(tempList)

    });
  }

  /**
   * [goZcmxDetail 详情页面]
   * @param {[type]} item [description]
   */
  goZcmxDetail(item) {
    //console.log(_.find(item, { "fname": "详情ID" }));
    var bosid = _.find(item, { "fname": "详情ID" })["fvalue"];
    //console.log(_.find(item, { "fname": "详情ID" })["fvalue"]);
    if (bosid) {
      let modal = this.modalCtrl.create(ZcmxPage, {
        "bosid": bosid,
      });
      modal.present();

      modal.onWillDismiss((data: any) => {
      });
    }
  }
}
